package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-08-11
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 查询分类品牌列表
     *
     * @return
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList() {
        return baseMapper.selectCategoryBrandList();
    }


    /**
     * 新增分类品牌关系
     *
     * @param categoryBrand
     */
    @Override
    public void saveCategoryBrand(CategoryBrand categoryBrand) {
        //1. 根据品牌ID + 分类ID查询是否存在 -> 存在：抛出异常
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId());
        long count = count(queryWrapper);
        if (count > 0) {
            throw new ServiceException("分类品牌关系已存在");
        }
        //2. 保存分类品牌关系
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        baseMapper.insert(categoryBrand);
    }

    /**
     * 根据三级分类ID查询品牌列表（有两种实现方式）
     * @param categoryId
     * @return
     */
    //方式一：
    /*@Override
    public List<Brand> brandList(Long categoryId) {
        //方式一：采用wrapper查询
        List<CategoryBrand> categoryBrandList =
                this.list(new LambdaQueryWrapper<CategoryBrand>()
                        .eq(CategoryBrand::getCategoryId, categoryId)
                        .select(CategoryBrand::getBrandId));
        if(!CollectionUtils.isEmpty(categoryBrandList)){
            //采用stream流获取品牌ID列表
            *//*List<Long> brandIdList = categoryBrandList.stream()
                    //映射，处理流中每一项数据 由CategoryBrand类转为Long类型
                    //Function函数式接口：T入参类型，R出参类型
                    .map(categoryBrand -> {
                        return categoryBrand.getBrandId();
                    }).collect(Collectors.toList());*//*

            List<Long> brandIdList = categoryBrandList.stream()
                    .map(CategoryBrand::getBrandId)
                    .collect(Collectors.toList());
            //根据品牌ID列表查询品牌列表
            List<Brand> brandList = brandMapper.selectBatchIds(brandIdList);
            return brandList;
        }
        return null;
    }*/
     /**
     * 根据三级分类ID查询品牌列表（有两种实现方式）
     * @param categoryId
     * @return
     */
     //方式二：动态SQL（关联查询品牌列表）
     @Override
     public List<Brand> brandList(Long categoryId) {
         return baseMapper.brandList(categoryId);
     }

}
